# -*- coding: utf-8 -*-

from odoo import models, fields, api


class ResPartner(models.Model):
    _inherit = 'res.partner'
    _order = 'id desc,parent_id'

    total_in_amount=fields.Float(compute='compute_total_payment')
    total_out_amount=fields.Float(compute='compute_total_payment')
    area=fields.Char(string='区域')
    partner_company_name=fields.Char(string=u'公司抬头')
    partner_company_address=fields.Char(string=u'公司地址')
    partner_company_bank_name=fields.Char(string=u'开户行')
    partner_company_bank_code=fields.Char(string='银行账号')

    _sql_constraints = [
        ('partner_name', 'unique (name)', "名字必须唯一"),
    ]

    def compute_total_payment(self):
        for r in self:
            in_ids=self.env['account.payment'].sudo().search([('partner_id','=',r.id),('state','not in',('draft','cancelled')),('payment_type','=','inbound')])
            out_ids=self.env['account.payment'].sudo().search([('partner_id','=',r.id),('state','not in',('draft','cancelled')),('payment_type','=','outbound')])
            r.total_in_amount=sum(p.amount for p in in_ids)
            r.total_out_amount=sum(p.amount for p in out_ids)